package com.shop.service.impl;

import com.github.pagehelper.PageInfo;
import com.shop.entity.Coupon;
import com.shop.mapper.CouponMapper;
import com.shop.service.CouponService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

@Service("couponService")
@RequiredArgsConstructor
@Transactional(rollbackFor = RuntimeException.class,
        propagation = Propagation.REQUIRED)
public class CouponServiceImpl implements CouponService {
    private final CouponMapper couponMapper;

    /**
     * 获取所有优惠券
     * @param pageNum 页码
     * @param pageSize 每页数量
     * @return 券
     */
    @Override
    public PageInfo<Coupon> listCoupon(Integer pageNum, Integer pageSize) {
        List<Coupon> list = couponMapper.listCoupon(pageNum, pageSize);
        return new PageInfo<>(list);
    }

    @Override
    public List<Coupon> listCouponDate(LocalDate end_date) {
        List<Coupon> list = couponMapper.listCouponDate(end_date);
        return new ArrayList<>(list);
    }

    @Override
    public List<Coupon> listCouponByStatus() {
        List<Coupon> list = couponMapper.listCouponByStatus();
        return new ArrayList<>(list);
    }


    /**
     * 根据id获取优惠券
     * @param id 优惠券id
     * @return 优惠券
     */
    @Override
    public Coupon getCouponById(Integer id) {
        return couponMapper.getCouponById(id);
    }
    /**
     * 添加优惠券
     * @param coupon 优惠券
     */
    @Override
    public void addCoupon(Coupon coupon) {
        couponMapper.addCoupon(coupon);
    }
    /**
     * 删除优惠券
     * @param id 优惠券id
     */
    @Override
    public void deleteCoupon(Integer id) {
        couponMapper.deleteCoupon(id);
    }
    /**
     * 更新优惠券
     * @param coupon 优惠券
     */
    @Override
    public void updateCoupon(Coupon coupon) {
        couponMapper.updateCoupon(coupon);
    }
    /**
     * 获取总数
     * @return 总数
     */
    @Override
    public int getTotal() {
        return couponMapper.getTotal();
    }
}
